<template>
  <div class="goods-image">
    <!-- middle之前放置大图盒子 -->
    <!-- 放大镜图片 -->
    <div
      class="large"
      v-show="!isOutside"
      :style="{
        backgroundImage: `url(${images[currentIndex]})`,
        backgroundPositionX: `${bg.x}px`,
        backgroundPositionY: `${bg.y}px`,
      }"
    ></div>
    <!-- 左侧大图 -->
    <div class="middle" ref="target">
      <img :src="images[currentIndex]" alt="" />
      <!-- 正方体蒙层滑块 -->
      <div
        v-show="!isOutside"
        class="layer"
        :style="{ left: left + 'px', top: top + 'px' }"
      ></div>
    </div>
    <!-- 右侧小图片列表 -->
    <ul class="small">
      <li
        v-for="(img, index) in images"
        :key="index"
        @mouseenter="currentIndex = index"
        :class="{ active: currentIndex === index }"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    /**
     * 需求: 鼠标经过某个小图,在作则显示他的大图
     * 步骤:
     * 1.鼠标经过获取当前图片的索引值
     * 2.根据索引值获取当前图片的uel
     */
    const currentIndex = ref(0)
    /**
     * 放大镜功能
     * 1.获取鼠标的坐标
     * 2.监听元素内鼠标移动的坐标变化 -> 控制蒙层移动 -> 计算蒙层: left和top值
     */
    /**
     * 思路❓：
     * 1. 大图和小图比例为2:1
     * 2. 所以计算公式应该为小图的定位距离乘以2，**大图背景*的移动始终是跟着滑块的移动走的**
     */
    const target = ref(null)
    // elementX, elementY是target元素范围内的坐标值,isOutside -> 鼠标是否在模板容器外
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // 滑块位置变量
    const left = ref(0) // 滑块距离左侧的距离
    const top = ref(0) // 滑块距离顶部的距离
    // 大图放大比例
    const bg = ref({ x: 0, y: 0 })
    watch([elementX, elementY, isOutside], () => {
      if (elementX.value < 100) {
        left.value = 0
      } else if (elementX.value > 300) {
        left.value = 200
      } else {
        left.value = elementX.value - 100
      }
      if (elementY.value < 100) {
        top.value = 0
      } else if (elementY.value > 300) {
        top.value = 200
      } else {
        top.value = elementY.value - 100
      }
      // 移动放大大图
      bg.value.x = -left.value * 2
      bg.value.y = -top.value * 2
    })
    return { currentIndex, target, elementX, elementY, isOutside, left, top, bg }
  }
}
</script>
<style scoped lang="less">
.large {
  position: absolute;
  top: 0;
  left: 412px;
  width: 400px;
  height: 400px;
  z-index: 500;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-repeat: no-repeat;
  background-size: 800px 800px;
  background-color: #f8f8f8;
}
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
